package com.thirdsdk.baidu;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;


/**
 * 百度地图展示类
 *
 * @author Administrator
 */
public class BaiduLocationActivity extends OrangeLifeBaseActivity implements
        OnGetGeoCoderResultListener,BaiduMap.OnMapStatusChangeListener{
    private TextView tv_title_right, tv_title;
    private ImageView ivMapCenter;
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private AddressListCallBack addressListCallBack;
    /**
     * 搜索模块，也可去掉地图模块独立使用
     */
    private GeoCoder mSearch = null;
    private LatLng ptCenter;
    private double lat;
    private double lon;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baidu_location);
        tv_title_right = (TextView) findViewById(R.id.tv_title_right);
        tv_title = (TextView) findViewById(R.id.tv_title);
        ivMapCenter = (ImageView) findViewById(R.id.ivMapCenter);
        tv_title.setText("地图");
        tv_title_right.setVisibility(View.GONE);
        if (getIntent().getExtras() != null) {
            lon = getIntent().getExtras().getFloat("longitude");
            lat = getIntent().getExtras().getFloat("latitude");
            initMap(R.id.bmapView);
            init(lat, lon);
        }
    }

    protected void init(double lat, double lon) {
        initGeoCoder(lat, lon);
    }

    protected void initMap(int id) {
        /* 地图初始化 */
        mMapView = (MapView) findViewById(id);
        mBaiduMap = mMapView.getMap();
        //地图状态改变相关监听
        mBaiduMap.setOnMapStatusChangeListener(this);
        setZoom();
    }
    
    protected void setAddressListCallBack(AddressListCallBack addressListCallBack){
        this.addressListCallBack = addressListCallBack;
    }
    private void initGeoCoder(double lat, double lon) {
        /** 初始化搜索模块，注册事件监听 */
        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(this);
        ptCenter = new LatLng(lat, lon);
        this.lat=lat;
        this.lon=lon;
        // 反Geo搜索
        mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(ptCenter));
    }

    @Override
    public void onGetGeoCodeResult(GeoCodeResult result) {
    }


    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(
                    BaiduLocationActivity.this,
                    getResources().getString(R.string.baidulocation_toast_text),
                    Toast.LENGTH_LONG).show();
            return;
        }
        mBaiduMap.clear();
        /*设置定位图标 */
//        mBaiduMap.addOverlay(new MarkerOptions().position(result.getLocation())
//                .icon(BitmapDescriptorFactory
//                        .fromResource(R.drawable.icon_marka)));
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(result
                .getLocation()));
        if(addressListCallBack!=null&&result.getPoiList()!=null) {
            addressListCallBack.getAddressList(result.getPoiList(),result.getLocation(),result.getAddress());
        }
    }

    /**
     * 设置比例尺
     */
    private void setZoom() {
        MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(17.0f);
        mBaiduMap.animateMapStatus(u);
    }


    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus) {
        if (ivMapCenter != null) {
            playJumpAnimation(thisDelta,ivMapCenter);
        }
    }

    @Override
    public void onMapStatusChange(MapStatus mapStatus) {

    }

    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {
        //地图操作的中心点
        LatLng cenpt = mapStatus.target;
        // 反Geo搜索
        mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(cenpt));

    }

    public void setImagev(ImageView v) {
       this.ivMapCenter=v;
    }
    private float thisDelta = 0.25f;
    // 跳起动画
    private void playJumpAnimation(final float delta,final View view) {
        final float height=view.getHeight();
        float originalY = 0;
        float finalY = 0 - height * delta;
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(new TranslateAnimation(0, 0, originalY,
                finalY));

        animationSet.setDuration(200);
        animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
        animationSet.setFillAfter(true);

        animationSet.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // 跳动提示可以上拉
                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        playLandAnimation(delta,view);
                    }
                }, 50);


            }
        });

        view.startAnimation(animationSet);
    }

    // 落下动画
    private void playLandAnimation(final float delta,final View view) {
        final float height=view.getHeight();
        float originalY = 0 - height * delta;
        float finalY = 0;
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(new TranslateAnimation(0, 0, originalY,
                finalY));

        animationSet.setDuration(100);
        animationSet.setInterpolator(new AccelerateInterpolator());
        animationSet.setFillAfter(true);

        animationSet.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
            }
        });

        view.startAnimation(animationSet);
    }
}
